package cn.ys.org.controller;

import cn.ys.base.util.AjaxResult;
import cn.ys.base.util.PageList;
import cn.ys.org.domain.SystemDictionaryDetail;
import cn.ys.org.query.SystemDictionaryDetailQuery;
import cn.ys.org.service.SystemDictionaryDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController  // @Controller + @ResponseBody
@RequestMapping("/sdd")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class SystemDictionaryDetailController {

    @Autowired
    private SystemDictionaryDetailService systemDictionaryDetailService;

//    /**
//     * 之前定义接口的玩法
//     * @return
//     */
//    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
//    public List<SystemDictionaryDetail> findAll(){
//        return systemDictionaryDetailService.findAll();
//    }

    /**
     * restful、 风格
     */
    @GetMapping
    public List<SystemDictionaryDetail> findAll(){
        return systemDictionaryDetailService.findAll();
    }

//    /**
//     * restful 风格 路径变量
//     *  如果前端传的参数名称 叫 deptId
//      http://localhost:8080/findById? id = 1
//     */
//    @RequestMapping("/findById")
//    public SystemDictionaryDetail findById(@RequestParam(value = "deptId",required = true) Long id){
//        return systemDictionaryDetailService.findById(id);
//    }

    /**
     * restful 风格 路径变量
     *  如果前端传的参数名称 叫 deptId
     *  http://localhost:8080/dept/1
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public SystemDictionaryDetail findById(@ApiParam(value="ID属性",required = true)  @PathVariable("id") Long id){
        return systemDictionaryDetailService.findById(id);
    }

    /**
     * 添加/ 修改
     * @return
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SystemDictionaryDetail systemDictionaryDetail){
        try {
            if (null == systemDictionaryDetail.getId()){
                // 添加
                systemDictionaryDetailService.add(systemDictionaryDetail);
            }else {
                // 编辑
                systemDictionaryDetailService.update(systemDictionaryDetail);
            }
        } catch (Exception e) {
//            throw new RuntimeException(e);
            return AjaxResult.me().fail();
        }
        return AjaxResult.me().success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        systemDictionaryDetailService.delete(id);
        return AjaxResult.me().success();
    }

    /**
     * 分页 json格式交互 字符串
     * @RequestBody 接收前端的json数据 转成我们的实体
     *
     */
    @PostMapping
    public PageList<SystemDictionaryDetail> queryByPage(@RequestBody SystemDictionaryDetailQuery quey){
        return systemDictionaryDetailService.queryByPage(quey);
    }

    // 批量删除
    @PatchMapping
    public AjaxResult piDeleteById(@RequestBody List<Long> ids){
        systemDictionaryDetailService.piDeleteById(ids);
        return AjaxResult.me().success();
    }

    @GetMapping("/tree")
    public List<SystemDictionaryDetail> deptTree() {
        return systemDictionaryDetailService.deptTree();
    }

    @GetMapping("/findDictionary/{sdId}")
    public List<SystemDictionaryDetail> findDictionary(@PathVariable("sdId") Integer sdId){
        return systemDictionaryDetailService.findDictionary(sdId);
    }
}
